package com.example.zhidao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhidao.dao.BlogMapper;
import com.example.zhidao.dao.CollectionsMapper;
import com.example.zhidao.dao.UserMapper;
import com.example.zhidao.model.po.Collections;
import com.example.zhidao.service.CollectionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;

@Service
public class CollectionsServiceImpl extends ServiceImpl<CollectionsMapper, Collections> implements CollectionsService {

    @Resource
    CollectionsMapper collectionsMapper;

    @Resource
    BlogMapper blogMapper;

    @Resource
    UserMapper userMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addColl(Collections collections) {
        try{
            collectionsMapper.insert(collections);
            blogMapper.addcollection(collections.getBid());
            userMapper.addcoll(collections.getUid());
        }catch (Exception e){
            // 回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delColl(Collections collections) {
        try{
            collectionsMapper.deleteById(collections);
            blogMapper.delcollection(collections.getBid());
            userMapper.delcoll(collections.getUid());
        }catch (Exception e){
            // 回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
}
